package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.User;
import cn.kgc.easybuy.service.UserService;
import cn.kgc.easybuy.utils.AjaxResult;
import cn.kgc.easybuy.utils.SecurityUtils;
import cn.kgc.easybuy.utils.SendMailUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-10 14:55
 **/
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @CheckPermission({"1"})
    @ApiOperation(value = "获取分页数据")
    @ApiImplicitParam(name = "params",value = "分页条件",required = true)
    @PostMapping("/selectUserByPage")
    public AjaxResult selectUserByPage(@RequestBody Map<String, Object> params){
        PageInfo<User> pageInfo = userService.selectUserByPage(params);
        if (pageInfo.getList().size()>0){
            return AjaxResult.success().add("pageInfo", pageInfo);
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @ApiOperation(value = "通过id获取用户对象")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @GetMapping("/selectUserById")
    public AjaxResult selectUserById(Integer id){
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }
        User user = userService.selectUserById(id);
        if (user == null) {
            return AjaxResult.error();
        }else {
            return AjaxResult.success().add("user",user);
        }

    }

    @CheckPermission({"1"})
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @GetMapping("/delUser")
    public  AjaxResult delUser(Integer id){
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }

        boolean flag = userService.delUser(id);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }

    }

    @CheckPermission({"1"})
    @ApiOperation(value = "添加用户")
    @ApiImplicitParam(name = "user",value = "用户对象",required = true)
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody User user){
        if (user == null) {
            throw new RuntimeException("返回的用户不能为空");
        }
        user.setPassword(SecurityUtils.md5Hex(user.getPassword()));
        user.setCreateTime(new Date());

        boolean flag = userService.addUser(user);
        if (flag) {
             return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "user",value = "用户对象",required = true)
    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody User user){
        if (user == null) {
            throw new RuntimeException("返回的用户不能为空");
        }
        if (user.getPassword() != null && StringUtils.isNotEmpty(user.getPassword())) {
            String password = SecurityUtils.md5Hex(user.getPassword());
            user.setPassword(password);
        }
        boolean flag = userService.updateUser(user);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @ApiOperation(value = "校验登录名唯一")
    @ApiImplicitParam(name = "loginName",value = "用户登录名",required = true)
    @GetMapping("/checkLoginName")
    public AjaxResult checkLoginName(String loginName){
        boolean flag = userService.checkLoginName(loginName);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验用户邮箱唯一")
    @ApiImplicitParam(name = "email",value = "用户邮箱",required = true)
    @GetMapping("/checkEmail")
    public AjaxResult checkEmail(String email){
        boolean flag = userService.checkEmail(email);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验用户身份证唯一")
    @ApiImplicitParam(name = "identityCode",value = "用户身份证",required = true)
    @GetMapping("/checkIdentityCode")
    public AjaxResult checkIdentityCode(String identityCode){
        boolean flag = userService.checkIdentityCode(identityCode);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验用户手机号唯一")
    @ApiImplicitParam(name = "mobile",value = "用户手机号",required = true)
    @GetMapping("/checkMobile")
    public AjaxResult checkMobile(String mobile){
        boolean flag = userService.checkMobile(mobile);
        if (flag) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验用户邮箱唯一")
    @ApiImplicitParam(name = "email",value = "用户邮箱",required = true)
    @GetMapping("/sendEmail")
    public AjaxResult sendEmail(String email){
        Random random = new Random();
        String code = 1000 + random.nextInt(9000)+"";
        redisTemplate.opsForValue().set("code"+code,code,2, TimeUnit.MINUTES);

        if (userService.sendEmail(email, code)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验验证码")
    @ApiImplicitParam(name = "code",value = "验证码",required = true)
    @GetMapping("/checkCode")
    public AjaxResult checkCode(String code){
        if (code.equals(redisTemplate.opsForValue().get("code"+code))) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "校验密码")
    @ApiImplicitParam(name = "user",value = "用户对象",required = true)
    @PostMapping("/checkPassword")
    public AjaxResult checkPassword(@RequestBody User user){
        String password = SecurityUtils.md5Hex(user.getPassword());
        user.setPassword(password);
        if (userService.checkPassword(user)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/selectUserByEmail")
    public AjaxResult selectUserByEmail(String email){
        User user = userService.selectUserByEmail(email);
        if (user != null) {
            return AjaxResult.success().add("user",user);
        }else {
            return AjaxResult.error();
        }

    }
}
